package com.bawei.mvvm.adapter;

import android.text.TextUtils;
import android.widget.ImageView;

import com.bawei.mvvm.R;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;

import androidx.databinding.BindingAdapter;

//图片适配器：dataBinding提供ImageViewBindingAdapter，但只支持本地图片，不支持网络图片
public class ImageViewNetBindingAdapter {
    @BindingAdapter("android:image")
    public static void setImageUrl(ImageView view, String imageUrl) {
       if(TextUtils.isEmpty(imageUrl)){
           view.setImageResource(R.drawable.ic_launcher_background);
       }else{
           Glide.with(view)
                   .load(imageUrl)
                   .placeholder(R.drawable.ic_launcher_background)
                   .transform(new CenterCrop())
                   .into(view);
       }
    }
}
